package com.mszlu.blog.service.mongo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mszlu.blog.common.Result;
import com.mszlu.blog.common.SysUserThreadLocal;
import com.mszlu.blog.dao.mongodb.MongoCommentDAO;
import com.mszlu.blog.entity.SysUser;
import com.mszlu.blog.entity.mongo.MongoComment;
import com.mszlu.blog.entity.mongo.dto.MongoCommentDto;
import com.mszlu.blog.service.SysUserServive;
import com.mszlu.blog.service.ThreadPoolService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MongoCommentService {
    //注入dao
    @Autowired
    private MongoCommentDAO mongoCommentDAO;

    @Autowired
    private SysUserServive sysUserServive;

    @Autowired
    private ThreadPoolService threadPoolService;

    public Result viewsByArticleId(Long id) {
        List<MongoComment> mongoComments = mongoCommentDAO.findByArticleIdaAndLevel(id,1);
        List<MongoCommentDto> mongoCommentDtos = mongoComments.stream().map((item)->{
            MongoCommentDto mongoCommentDto = new MongoCommentDto();
            BeanUtils.copyProperties(item,mongoCommentDto);
            SysUser author = sysUserServive.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getId, item.getAuthorId()));
            mongoCommentDto.setAuthor(author);

            List<MongoCommentDto> byArticleIdaAndLevel = mongoCommentDAO.findByParentIdAndLevel(item.getId(), 2).stream().map((t)->{
                MongoCommentDto mongoCommentDto1 = new MongoCommentDto();
                BeanUtils.copyProperties(t,mongoCommentDto1);

                SysUser author1 = sysUserServive.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getId, t.getAuthorId()));
                mongoCommentDto1.setAuthor(author1);

                SysUser toUser = sysUserServive.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getId, t.getToUid()));
                mongoCommentDto1.setToUser(toUser);

                return mongoCommentDto1;
            }).collect(Collectors.toList());

            mongoCommentDto.setChildrens(byArticleIdaAndLevel);

            return mongoCommentDto;
        }).collect(Collectors.toList());


        return Result.success(mongoCommentDtos);
    }



    public Result comment(MongoComment commentParam) {
        String content = commentParam.getContent();
        Long articleId = commentParam.getArticleId();
        String parentId = commentParam.getParentId();
        Long toUid = commentParam.getToUid();

        SysUser sysUser = SysUserThreadLocal.get();

        MongoComment mongoComment = new MongoComment();

        mongoComment.setContent(content);
        mongoComment.setArticleId(articleId);
        mongoComment.setAuthorId(sysUser.getId());
        mongoComment.setParentId(parentId);
        mongoComment.setToUid(toUid);
        mongoComment.setCreateDate(new Date());
        if (parentId == null) {
            mongoComment.setLevel(1);
            //更新article表的评论数量
            threadPoolService.updateCommentCounts(articleId);
        }else {
            mongoComment.setLevel(2);
        }

        MongoComment save = mongoCommentDAO.save(mongoComment);

        MongoCommentDto mongoCommentDto = new MongoCommentDto();

        BeanUtils.copyProperties(save, mongoCommentDto);

        SysUser author = sysUserServive.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getId, save.getAuthorId()));
        mongoCommentDto.setAuthor(author);

        SysUser toUser = sysUserServive.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getId, save.getToUid()));
        mongoCommentDto.setToUser(toUser);

        return Result.success(mongoCommentDto);
    }


    //插入并返回评论
    public MongoComment saveComment(MongoComment comment) {
        return mongoCommentDAO.save(comment);
    }

    public List<MongoComment> findCommentList() {
        return mongoCommentDAO.findAll();
    }
}
